﻿using System.Windows;
using WpfApp1.ViewModels;
using WpfApp1.ViewModels.Dialogs;
using WpfApp1.Views;

namespace WpfApp1
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void OnInitialized()
        {
            //https://github.com/PrismLibrary/Prism/blob/master/src/Wpf/Prism.Wpf/Dialogs/DialogService.cs
            DialogParameters parameters = new DialogParameters
            {
                { KnownDialogParameters.WindowName, "myWindow" }
            };
            var dialog = Container.Resolve<IDialogService>();
            dialog.ShowDialog(nameof(LoginView), parameters, callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                //IMPORTANT
                base.OnInitialized();
            });
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            /**
                通过 RegisterDialogWindow 进行对话父类窗口类型的注册后，
            会优先使用，而不会自动使用默认的对话窗口类型对象。
            RegisterDialogWindow<TWindow>([string name])：注册对话的父类窗口类型。
            当注册多个父类窗口类型时，可以传入name方便后面展示时通过key来指定窗口类型，
            需要注意注册时一旦指定了name，展示时就必须要指定name，
            否则找不到对应的父类窗口对象。

            原文链接：https://blog.csdn.net/jjailsa/article/details/135972127
            */
            containerRegistry.RegisterDialogWindow<MyDialogWindow>("myWindow");
            containerRegistry.RegisterDialogWindow<CustomWindow>("customWindow");

            containerRegistry.RegisterDialog<LoginView, LoginViewModel>(nameof(LoginView));

            containerRegistry.RegisterDialog<NotificationDialog, NotificationDialogViewModel>();

            containerRegistry.RegisterForNavigation<HomeView, HomeViewModel>(nameof(HomeView));
            containerRegistry.RegisterForNavigation<UserView, UserViewModel>(nameof(UserView));
            containerRegistry.RegisterForNavigation<RoleView, RoleViewModel>(nameof(RoleView));
            containerRegistry.RegisterForNavigation<MenuView, MenuViewModel>(nameof(MenuView));
        }

        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();

            ViewModelLocationProvider.Register<MainWindow, MainViewModel>();
        }
    }
}
